package com.aplink.generic.widgets;

import java.util.ArrayList;

import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.ConfigAdapter;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.ColumnSetting;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.gridview.GridViewSpec;
import com.aplink.generic.util.Language;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.objectrule.Target;
import com.aplink.generic.widgets.util.SwipeItemDeletion;
import com.aplink.generic.xmlparser.ColumnSettingParser;
import com.aplink.sg.R;

public class ApLinkGridView extends ApLinkBaseView implements
        OnItemClickListener {
	private ArrayList<ColumnSetting> columnSettings;
	private ContentData mContentData;
	private ArrayList<GenericObject> mGenericObjects;
	private GridView mGridView;
	private ConfigAdapter mGridViewAdapter;
	private GridViewSpec mGridViewSpec;

	public ApLinkGridView(GenericFragment genericFragment,
	        ControlSpec controlSpec, View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		mGridViewSpec = (GridViewSpec) controlSpec;
		mGridView = (GridView) view;
		mContentData = mGridViewSpec.getContentData();
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	private ConfigAdapter generateConfigAdapter() {
		this.columnSettings = ColumnSettingParser.parse(getGenericFragment(),
		        this.mGridViewSpec.getRptrdesignID());
		if (mGridViewSpec.isSwipeToRemove()) {
			SwipeItemDeletion mTouchListener = new SwipeItemDeletion(
			        getApplicationContext(), mGridView);
			return new ConfigAdapter(getGenericFragment(),
			        this.mGenericObjects, this.columnSettings,
			        this.mGridViewSpec.getRowItem(), mTouchListener);
		}
		return new ConfigAdapter(getGenericFragment(), this.mGenericObjects,
		        this.columnSettings, this.mGridViewSpec.getRowItem());
	}

	@Override
	public GenericAttribute getAttribute() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public View getView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void initViewProperties() {
		if (!TextUtils.isEmpty(mGridViewSpec.getEmptyMessage())) {
			View emptyView = getLayoutInflater().inflate(R.layout.empty_view,
			        mGridView, false);
			TextView textView = (TextView) emptyView
			        .findViewById(R.id.empty_view_title);
			textView.setText(Language.getInstance().get(
			        mGridViewSpec.getEmptyMessage()));
			textView.setCompoundDrawablesWithIntrinsicBounds(0,
			        mGridViewSpec.getEmptyIconID(), 0, 0);
			((ViewGroup) mGridView.getParent()).addView(emptyView);
			mGridView.setEmptyView(emptyView);
		}

		if (!mGridViewSpec.getContextMenus().isEmpty()) {
			mGridView
			        .setTag(R.id.CONTEXT_MENU, mGridViewSpec.getContextMenus());
			getGenericFragment().registerForContextMenu(mGridView);
		}
	}

	@Override
	public void languageChanged() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void loadBindingObject() {
		SqlMethods whereClause = getGenericFragment().getSqlMethods();
		this.mGenericObjects = getDatabaseGet().getList(this.mContentData,
		        whereClause == null ? null : whereClause.toString());
		this.mGridViewAdapter = generateConfigAdapter();
		this.mGridView.setAdapter(this.mGridViewAdapter);
	}

	@Override
	protected void loadDefaultObject() {
		this.mGenericObjects = getDatabaseGet()
		        .getList(this.mContentData, null);
		this.mGridViewAdapter = generateConfigAdapter();
		this.mGridView.setAdapter(this.mGridViewAdapter);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
	        long id) {
		if (mGridViewSpec.getTargetCollection().isNotEmpty()) {
			final String fullClassName = null;
			try {
				final GenericObject genericObject = mGridViewAdapter
				        .getDisplayObjects().get(position);
				final Target targetActivity = mGridViewSpec
				        .getTargetCollection().getTargetClass(genericObject);
				getGenericFragment().startActivity(
				        targetActivity.getTargetClass(),
				        targetActivity.getPassingValue(), genericObject);
			} catch (final ClassNotFoundException e) {
				LogUtil.error(fullClassName + " is not found",
				        "List view item click", getClass());
			} catch (final NameNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void setEventListener() {
		mGridView.setOnItemClickListener(this);
	}

	@Override
	public void setObjPropInfoTemp(OnGetObjProInfo getObjProInfo) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnClickListener(OnClickListener onClickListener) {
		// TODO Auto-generated method stub

	}

}
